package com.cloudeye.business.mq.config;

import cn.hutool.crypto.SecureUtil;
import com.cloudeye.business.mq.service.MqMsgSenderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;

/**
 * @Author: lafangyuan
 * @Date: 2021/12/6 10:21
 */
@Slf4j
@Configuration
@ConditionalOnProperty(value = "rabbitmq.username",matchIfMissing = false)
public class MyRabbitMqConfiguration {

    private static final String AES_KEY = "J@ko4%dlAM9#PnE!";

    @Bean
    RabbitMqProperties rabbitMqProperties(){
        return new RabbitMqProperties();
    }
    @Bean(name = "myConnectionFactory" )
    public ConnectionFactory myConnectionFactory(RabbitMqProperties properties){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(properties.getHost());
        connectionFactory.setPort(properties.getPort());
        connectionFactory.setUsername(properties.getUsername());
        connectionFactory.setPassword(decrypt(properties.getPassword()));
        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
        return connectionFactory;
    }
    @Bean
    public RabbitTemplate rabbitTemplate(@Qualifier(value = "myConnectionFactory") ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate =  new RabbitTemplate(connectionFactory);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
               if (!ack){
                   log.error("消息发送失败");
               }
            }
        });
        return  rabbitTemplate;
    }

    @Bean
    public RabbitAdmin rabbitAdmin(@Qualifier(value = "myConnectionFactory") ConnectionFactory connectionFactory){
        return new RabbitAdmin(connectionFactory);
    }
    protected String decrypt(String encoded){
        if (StringUtils.isEmpty(encoded)){
            return encoded;
        }
        return SecureUtil.aes(AES_KEY.getBytes(StandardCharsets.UTF_8)).decryptStr(encoded);
    }

    @Bean
    public MqMsgSenderService mqMsgSenderService(){
        return new MqMsgSenderService();
    }
    public static void main(String[] args) {
        String password = "870918";
        System.out.println(SecureUtil.aes(AES_KEY.getBytes(StandardCharsets.UTF_8)).encryptBase64(password));
    }
}
